//
//  CZQHomePageChileOneVC.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/22.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQHomePageChileOneVC.h"
#import "CZQCoachHeadTCell.h"
#import "CZQHomePageSectionHead.h"
#import "CZQHomeAbourTableViewCell.h"
#import "CZQHomeTimeTableViewCell.h"
#import "CZQHomeCourseTCell.h"
#import "CZQHomeVIPCommentCell.h"
#import "CZQShareViewController.h"
#import "CZQHomeAboutCoachVC.h"
#import "CZQCommentDetaiVC.h"
#import "CZQClubListVC.h"
#import "CZQHomePageChildTwoVC.h"
#import "CZQHaveTimeView.h"
#import "CZQHomeStudyDetailVC.h"
#import "CZQCoachMoveDetailVC.h"
#import "CZQCoachDetailModel.h"
@interface CZQHomePageChileOneVC ()<UITableViewDelegate,UITableViewDataSource>
/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

/**
 教练详情模型数据
 */
@property(nonatomic,strong)CZQCoachDetailModel *model;
@end

@implementation CZQHomePageChileOneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self loadData];
    
    //获取本地数据
    self.model = [CZQCoachDetailModel model];
    
    [self.tableView setNeedsLayout];
    [self.tableView layoutIfNeeded];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [self loadData];
    
    [self loadMemberDataList];
}

#pragma mark - 学员案例列表

-(void)loadMemberDataList
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    
    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"ShopID"] forKey:@"ShopID"];
    
    [params setObject:@1 forKey:@"page"];
    
    [params setObject:@100 forKey:@"rows"];
    
    
    
    [HTTPTool GET:Get_GetMemberCaseListData parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              NSLog(@"%@",responseObject);
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSArray *resultArr = [responseObject objectForKey:@"rows"];
                  
                  self.dataArr = [NSMutableArray arrayWithArray:resultArr];
                  
                  NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
                  
                  [self.tableView reloadSections:set withRowAnimation:(UITableViewRowAnimationFade)];
              }
              
              
          }
     
          failure:^(NSError *error){
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
    
}

-(void)loadData{
    
    //Get_GetCourseWareListData
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    if (self.cycleModel) {
        [params setObject:self.cycleModel.MemberID forKey:@"teacherid"];
    }else
    {
        [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"UserID"] forKey:@"teacherid"];
    }
    
    
    
    
    
    [HTTPTool GET:Get_Teacher_GetTeacherInfo parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              NSLog(@"%@",responseObject);
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  CZQCoachDetailModel *model = [CZQCoachDetailModel mj_objectWithKeyValues:[responseObject objectForKey:@"User"]];
                  
                  [CZQCoachDetailModel saveModel:model];
                  
                  self.model = model;
                  
                  
                  [self.tableView reloadData];
              }
              
              
          }
     
          failure:^(NSError *error){
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}
#pragma mark -- 懒加载

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        tableView.backgroundColor = BackGroundColor;
        
        tableView.estimatedRowHeight = 150;
        
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
        }];
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册
        [tableView registerClass:[CZQHomeTimeTableViewCell class] forCellReuseIdentifier:@"CZQHomeTimeTableViewCell"];
        
        [tableView registerClass:[CZQHomeCourseTCell class] forCellReuseIdentifier:@"CZQHomeCourseTCell"];
        
        [tableView registerClass:[CZQHomeVIPCommentCell class] forCellReuseIdentifier:@"CZQHomeVIPCommentCell"];
        
        [tableView registerNib:[UINib nibWithNibName:@"CZQCoachHeadTCell" bundle:nil]  forCellReuseIdentifier:@"cell"];
        
        [tableView registerNib:[UINib nibWithNibName:@"CZQHomeAbourTableViewCell" bundle:nil]  forCellReuseIdentifier:@"CZQHomeAbourTableViewCell"];
        //        [tableView registerNib:[UINib nibWithNibName:@"YDTrainingTableViewCell" bundle:nil]  forCellReuseIdentifier:@"cell"];
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 6;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 5) {
        return 2;
    }
    return 1;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WS(weakSelf);
    
    if (indexPath.section == 0) {
        CZQCoachHeadTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.iconImageView sd_setImageWithURL:[Helper imageUrlStringToChinese:self.model.Pic imageType:nil] placeholderImage:PlaceHold];
        cell.nameLable.text = self.model.RealName;
        cell.courseCountLable.text = [NSString stringWithFormat:@"累计上课%@节",self.model.Orders];
        cell.coachClassLable.text = [NSString stringWithFormat:@"%@认证教练",self.model.Available];
        [cell.goodLable setTitle:[NSString stringWithFormat:@"%@好评",self.model.Orders]];
        cell.starView.allowTouch = NO;
        cell.starView.scorePercent = [self.model.Ratio integerValue];
        cell.starView.allowIncompleteStar = YES;
        NSArray *cerArr = [[self.model.GetResume objectForKey:@"CertificateExp"] mj_JSONObject];
        cell.cerCountLable.text = [NSString stringWithFormat:@"%ld",cerArr.count];
        cell.studyLable.text = [NSString stringWithFormat:@"%ld",self.dataArr.count];
        
        NSLog(@"%@",self.dataArr);
        
        //学员案例
        [cell.studyLable bk_removeAllBlockObservers];
        [cell.studyLable bk_whenTapped:^{
            CZQHomeStudyDetailVC *studyD = [CZQHomeStudyDetailVC new];
            
            if (weakSelf.dataArr.count > 0) {
                studyD.studyArrM = weakSelf.dataArr;
                [weakSelf.navigationController pushViewController:studyD animated:YES];
            }
            
            
        }];
        
        //动态
        [cell.dynamicLable bk_removeAllBlockObservers];
        [cell.dynamicLable bk_whenTapped:^{
            CZQCoachMoveDetailVC *detal = [CZQCoachMoveDetailVC new];
            
            if (weakSelf.cycleModel) {
                detal.cycleModel = weakSelf.cycleModel;
            }
            
            
            [weakSelf.navigationController pushViewController:detal animated:YES];
        }];
        
        return cell;
    }else if (indexPath.section == 1){
        
        CZQHomeAbourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZQHomeAbourTableViewCell"];
        
        cell.longLable.text = self.model.FavTags;
        
        cell.titleLable.text = self.model.Memo;
        return cell;
    }else if (indexPath.section == 2){
        CZQHomeTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZQHomeTimeTableViewCell"];
        
        cell.arrM = @[@"今日可约",@"今日可约今日可约今日可约"];
        
        return cell;
        
    }else if (indexPath.section == 3){
        
        CZQHomeTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZQHomeTimeTableViewCell"];
        
        
//        NSArray *placeArr = [CZQGetShopPlaceModel mj_objectArrayWithKeyValuesArray:self.model.GetShopPlace];
//
//        for (CZQGetShopPlaceModel *model in placeArr) {
//            <#statements#>
//        }
        
        NSLog(@"%@",self.model.GetShopPlace);
        
        cell.arrM = self.model.GetShopPlace;
        
        
        return cell;
        
        
    }else if (indexPath.section == 4){
        
        CZQHomeCourseTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZQHomeCourseTCell"];
        
        
        
        cell.dataArrM = self.model.GetCoursePlan;
        
        return cell;
        
        
    }else if (indexPath.section == 5){
        
        CZQHomeVIPCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZQHomeVIPCommentCell"];
        
        return cell;
        
    }
    
    
    CZQCoachHeadTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    return cell  ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section > 0) {
        return 50;
    }
    
    return CGFLOAT_MIN;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 5) {
        return 50;
    }
    return 10;
}

//-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (indexPath.section == 4) {
//        return 500;
//    }return 150;
//
//}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    if (section == 5) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"更多"];
        [btn setTitleColor:CSColorFromRGB(0x666666)];
        btn.backgroundColor = CSWhiteColor;
        btn.frame = CGRectMake(0, 0, kScreenWidth, 50);
        
        return btn;
    }
    UIView *view = [[UIView alloc]init];
    
    view.backgroundColor = BackGroundColor;
    
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section > 0) {
        
        CZQHomePageSectionHead *sectionHead = [CZQHomePageSectionHead new];
        
        if (section == 1) {
            //关于他
            
            sectionHead.titleLabe.text = @"关于TA";
            sectionHead.howLabe.text = @"";
            [sectionHead.moreBtn addTarget:self action:@selector(about)];
        }
        else if (section == 2)
        {
            
            //可约时间
            sectionHead.titleLabe.text = @"可约时间";
            sectionHead.howLabe.text = @"· 近七天";
            [sectionHead.moreBtn addTarget:self action:@selector(time)];
            
        }
        else if (section == 3)
        {
            //服务场地
            sectionHead.titleLabe.text = @"服务场地";
            sectionHead.howLabe.text = [NSString stringWithFormat:@"· %ld个场地",self.model.GetShopPlace.count];
            [sectionHead.moreBtn addTarget:self action:@selector(club)];
            
        }
        else if (section == 4)
        {
            //所授课程
            sectionHead.titleLabe.text = @"所授课程";
            sectionHead.howLabe.text = [NSString stringWithFormat:@"· %ld种",self.model.GetCoursePlan.count];
            [sectionHead.moreBtn addTarget:self action:@selector(course)];
            
        }
        else if (section == 5)
        {
            //会员评价
            sectionHead.titleLabe.text = @"会员评价";
            sectionHead.howLabe.text = @"";
            [sectionHead.moreBtn addTarget:self action:@selector(commentDetai)];
            
        }
        
        return sectionHead;
        
        
    }
    
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - 关于他
-(void)about
{
    CZQHomeAboutCoachVC *coachVc = [CZQHomeAboutCoachVC new];
    
    coachVc.studyArrM = self.dataArr;
    
    
    
    coachVc.model = self.model;
    
    [self.navigationController pushViewController:coachVc animated:YES];
    
}
#pragma mark - 可约时间
-(void)time
{
    
    CZQHaveTimeView *haveTimeView = [[CZQHaveTimeView alloc] init];
    

    
    __weak __typeof(self) weakSelf = self;
    
    haveTimeView.commitBlock = ^(NSString *name, NSString *unit) {
        
        
    };
    
    [haveTimeView showWithAnimation];
}
#pragma mark - /服务场地
-(void)club
{
    CZQClubListVC *list = [CZQClubListVC new];
    
    [self.navigationController pushViewController:list animated:YES];
    
}
#pragma mark - 所授课程
-(void)course
{
    //课程
    CZQHomePageChildTwoVC *courseVC = [CZQHomePageChildTwoVC new];
    
    [self.navigationController pushViewController:courseVC animated:YES];
    
}
#pragma mark - 会员评价
-(void)commentDetai
{
    CZQCommentDetaiVC *comment = [CZQCommentDetaiVC new];
    
    [self.navigationController pushViewController:comment animated:YES];
    
}
#pragma mark - 分享
-(void)share
{
    CZQShareViewController *share = [CZQShareViewController new];
    
    [self.navigationController pushViewController:share animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
